package com.ct.web.controllers.auxiliary;

import org.hibernate.classic.Session;
import org.hibernate.SessionFactory;
import org.hibernate.jdbc.Work;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.sql.Connection;
import java.sql.CallableStatement;

/**
 * Created by IntelliJ IDEA.
 * User: nmkravchenko
 * Date: 29.10.2009
 * Time: 16:05:25
 * To change this template use File | Settings | File Templates.
 */
public class DBSchemaUpdater {
    private final static Log LOG = LogFactory.getLog(DBSchemaUpdater.class);
    private static final String PROC_NAME = "import_gps_fmt_data";
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public void updateDBSchema(){
        LOG.debug("Starting DBSchemaUpdater cron task ...");
        Session session = null;
        try{
            session = sessionFactory.openSession();
            session.doWork(new Work(){
                public void execute(Connection connection) throws java.sql.SQLException{
                    LOG.debug("Running proc '" + PROC_NAME + "' ");
                    CallableStatement stmt = connection.prepareCall("{call " + PROC_NAME + "}");
                    stmt.execute();
                }
            });
        }catch(Exception e){
            LOG.error(e);
        }finally{
            if(session != null){
                session.close();
            }
        }
        
    }
}
